<?php

namespace app\checkbill\logic;
use app\common\library\RedissLock;
use app\common\service\CommonSystemImportService;
use think\facade\Db;
use app\checkbill\logic\CheckMonthOldLogic;
class CheckMonthFeeLogic extends BasicLogic
{




    /**
     * @desc   写入导入模版数据
     * @author luckyXiong
     * @date   2024-06-07
     */
    public function add_template_month($first_info=[],$full_url='',$importType=''){
        if($importType==108){
            $type     = $first_info['D']??'';
            $order_id = $first_info['E']??'';
            $relate_order_id = $first_info['AO']??'';
        }else{
            $type     = $first_info['F']??'';
            $order_id = $first_info['G']??'';
            $relate_order_id = $first_info['AL']??'';
        }
        if($type != 'Order'){
            $order_id = $relate_order_id;
        }
        $order_info = Db::name('order')->where('order_id',$order_id)->field('shop_id,createtime')->find();
        $create_time= $order_info['createtime']??'';
        $shop_id    = $order_info['shop_id']??'';
        $date       = date("Y-m",$create_time);
        $bach_id    = (new CheckMonthOldLogic())->get_last_month().$shop_id;
        $add_data = [
                'batch_id'     =>$bach_id,
                'date'         =>$date,
                'shop_id'      =>$shop_id,
                'import_status'=>1,
                'source_url'   =>$full_url,
                'create_time'  =>date("Y-m-d H:i:s", time()),
            ];

        $exist_date =  Db::name('month_check')->where(['shop_id'=>$shop_id,'date'=>$date])->find();
        if(!$exist_date){
            Db::name('month_check')->insert($add_data);
        }
    }


    /**
     * @desc   月度账单导入新模版
     * @date    2024-06-11
     * @author  luckyXiong
     */
    public function uploadInfo($params){
        try{
           CommonSystemImportService::staticModel()->operate_import_data([
                    $this,//当前对应的service
                    'save',//类型: template 下载模板; save :保存 导入
                    CommonSystemImportService::TYPE_IMPORT_MONTH_NEW,
                    1,
                    $params
                ]
            );
        }catch (\Exception $e) {
            return $this->jsonMessage(201, false, $e->getMessage(), null);
        }
    }



    /**
     * @desc    月度账单导入老模版
     * @date    2024-06-11
     * @author  luckyXiong
     */
    public function import_info($params=[]){
        $key = 'import_info_month_new::'.time();
        $redis_lock                          = new RedissLock();
        $lock                                = $redis_lock::lock($key, $redis_lock::UNBLOCK, 500);

        $order_id                            = getIsset($params,'order_id');
        $quantity                            = getIsset($params,'quantity');
        $sku_name                            = getIsset($params,'sku_name');
        $total_settlement_amount             = getIsset($params,'total_settlement_amount');
        $net_sales                           = getIsset($params,'net_sales');
        $gross_sales                         = getIsset($params,'gross_sales');
        $gross_sales_refund                  = getIsset($params,'gross_sales_refund');
        $seller_discount                     = getIsset($params,'seller_discount');
        $seller_discount_refund              = getIsset($params,'seller_discount_refund');
        $shipping                            = getIsset($params,'shipping');
        $tiktok_shop_shipping_fee            = getIsset($params,'tiktok_shop_shipping_fee');
        $customer_paid_shipping_fee          = getIsset($params,'customer_paid_shipping_fee');
        $customer_paid_shipping_fee_refund   = getIsset($params,'customer_paid_shipping_fee_refund');
        $tiktok_shop_shipping_incentive      = getIsset($params,'tiktok_shop_shipping_incentive');


        $tiktok_Shop_shipping_incentive_refund = getIsset($params,'tiktok_Shop_shipping_incentive_refund');
        $shipping_fee_subsidy                = getIsset($params,'shipping_fee_subsidy');
        $return_shipping_fee                 = getIsset($params,'return_shipping_fee');
        $fees                                = getIsset($params,'fees');
        $referral_fee                        = getIsset($params,'referral_fee');
        $refund_administration_fee           = getIsset($params,'refund_administration_fee');
        $affiliate_commission                = getIsset($params,'affiliate_commission');
        $ajustment_amount                    = getIsset($params,'ajustment_amount');
        $customer_payment                    = getIsset($params,'customer_payment');
        $customer_refund                     = getIsset($params,'customer_refund');
        $platform_discounts                  = getIsset($params,'platform_discounts');
        $platform_discounts_refund           = getIsset($params,'platform_discounts_refund');

        $sales_tax_payment                   = getIsset($params,'sales_tax_payment');
        $sales_tax_refund                    = getIsset($params,'sales_tax_refund');
        $retail_delivery_fee_payment         = getIsset($params,'retail_delivery_fee_payment');
        $retail_delivery_fee_refund          = getIsset($params,'retail_delivery_fee_refund');
        $customer_paid_shipping_fee_before_discounts = getIsset($params,'customer_paid_shipping_fee_before_discounts');
        $seller_shipping_fee_discount        = getIsset($params,'seller_shipping_fee_discount');
        $tiktok_Shop_shipping_fee_discount_to_customer = getIsset($params,'tiktok_Shop_shipping_fee_discount_to_customer');
        $related_order_id                    = getIsset($params,'related_order_id');
        $type                                = getIsset($params,'type');
        $statement_date                      = getIsset($params,'statement_date');
        $statement_id                        = getIsset($params,'statement_id');
        $payment_id                          = getIsset($params,'payment_id');



        if($type!='Order'){
            $order_id = $related_order_id;
        }

        $shop_info            = Db::name('order')->where('order_id',$order_id)->field('createtime,shop_id')->find();
        $shop_id              = $shop_info['shop_id']??0;
        $bach_id              = self::get_last_month().$shop_id;
        $create_time          = date("Y-m-d H:i:s", $shop_info['createtime']);

        //数据入库逻辑
        $info = [
            'batch_id'                         => $bach_id,
            'shop_id'                          => $shop_info['shop_id']??0,
            'order_id'                         => $order_id,
            'sku_name'                         => $sku_name,
            'quantity'                         => $quantity,
            'sku_num'                          => get_sku_nums($sku_name),
            'total_settlement_amount'          => $total_settlement_amount,
            'net_sales'                        => $net_sales,
            'gross_sales'                      => $gross_sales,
            'gross_sales_refund'               => $gross_sales_refund,
            'seller_discount'                  => $seller_discount,
            'seller_discount_refund'           => $seller_discount_refund,
            'shipping'                         => $shipping,
            'tiktok_shop_shipping_fee'         => $tiktok_shop_shipping_fee,
            'customer_paid_shipping_fee'       => $customer_paid_shipping_fee,
            'customer_paid_shipping_fee_refund' => $customer_paid_shipping_fee_refund,
            'tiktok_shop_shipping_incentive'    => $tiktok_shop_shipping_incentive,
            'tiktok_Shop_shipping_incentive_refund' => $tiktok_Shop_shipping_incentive_refund,
            'shipping_fee_subsidy'             => $shipping_fee_subsidy,
            'return_shipping_fee'              => $return_shipping_fee,
            'fees'                             => $fees,
            'referral_fee'                     => $referral_fee,
            'refund_administration_fee'        => $refund_administration_fee,
            'affiliate_commission'             => $affiliate_commission,
            'ajustment_amount'                 => $ajustment_amount,
            'customer_payment'                 => $customer_payment,
            'customer_refund'                  => $customer_refund,
            'platform_discounts'               => $platform_discounts,
            'platform_discounts_refund'        => $platform_discounts_refund,
            'sales_tax_payment'                => $sales_tax_payment,
            'sales_tax_refund'                 => $sales_tax_refund,
            'retail_delivery_fee_payment'      => $retail_delivery_fee_payment,
            'retail_delivery_fee_refund'       => $retail_delivery_fee_refund,
            'customer_paid_shipping_fee_before_discounts' => $customer_paid_shipping_fee_before_discounts,
            'seller_shipping_fee_discount'     => $seller_shipping_fee_discount,
            'tiktok_Shop_shipping_fee_discount_to_customer' => $tiktok_Shop_shipping_fee_discount_to_customer,
            'related_order_id'                 => $related_order_id,
            'type'                             => $type,
            'statement_date'                   => $statement_date,
            'statement_id'                     => $statement_id,
            'payment_id'                       => $payment_id,
            'date'                             => date("Y-m",$shop_info['createtime']),
            'create_time'                      => $create_time,
        ];

        $exist_info = Db::name('month_check_new_log')->where(['order_id'=>$order_id,'shop_id'=>$shop_id])->value('id');
        if($exist_info){
            return  true;
        }

        $res = Db::name('month_check_new_log')->insertGetId($info);
        $lock->unlock();
        $info['status'] = $res == 1 ? 1:2;
        return  $info;

    }



    /**
     * @desc    月度账单导入老模版
     * @date    2024-06-11
     * @author  luckyXiong
     */
    public function get_excel_row_data($row){
        return [
            'order_id'                                          => empty($row['G'])? '': trim($row['G']),
            'sku_name'                                          => empty($row['K'])? '': trim($row['K']),
            'quantity'                                          => empty($row['I'])? '': trim($row['I']),
            'total_settlement_amount'                           => empty($row['N'])? '': trim($row['N']),
            'net_sales'                                         => empty($row['O'])? '': trim($row['O']),
            'gross_sales'                                       => empty($row['P'])? '': trim($row['P']),
            'gross_sales_refund'                                => empty($row['Q'])? '': trim($row['Q']),
            'seller_discount'                                   => empty($row['R'])? '': trim($row['R']),
            'seller_discount_refund'                            => empty($row['S'])? '': trim($row['S']),
            'shipping'                                          => empty($row['T'])? '': trim($row['T']),
            'tiktok_shop_shipping_fee'                          => empty($row['U'])? '': trim($row['U']),
            'customer_paid_shipping_fee'                        => empty($row['X'])? '': trim($row['X']),
            'customer_paid_shipping_fee_refund'                 => empty($row['Y'])? '': trim($row['Y']),
            'tiktok_shop_shipping_incentive'                    => empty($row['Z'])? '': trim($row['Z']),
            'tiktok_Shop_shipping_incentive_refund'             => empty($row['AA'])? '': trim($row['AA']),
            'shipping_fee_subsidy'                              => empty($row['AB'])? '': trim($row['AB']),
            'return_shipping_fee'                               => empty($row['AC'])? '': trim($row['AC']),
            'fees'                                              => empty($row['AD'])? '': trim($row['AD']),
            'referral_fee'                                      => empty($row['AF'])? '': trim($row['AF']),
            'refund_administration_fee'                         => empty($row['AG'])? '': trim($row['AG']),
            'affiliate_commission'                              => empty($row['AH'])? '': trim($row['AH']),
            'ajustment_amount'                                  => empty($row['AJ'])? '': trim($row['AJ']),
            'customer_payment'                                  => empty($row['AN'])? '': trim($row['AN']),
            'customer_refund'                                   => empty($row['AO'])? '': trim($row['AO']),
            'platform_discounts'                                => empty($row['AR'])? '': trim($row['AR']),
            'platform_discounts_refund'                         => empty($row['AS'])? '': trim($row['AS']),
            'sales_tax_payment'                                 => empty($row['AV'])? '': trim($row['AV']),
            'sales_tax_refund'                                  => empty($row['AW'])? '': trim($row['AW']),
            'retail_delivery_fee_payment'                       => empty($row['AX'])? '': trim($row['AX']),
            'retail_delivery_fee_refund'                        => empty($row['AY'])? '': trim($row['AY']),
            'customer_paid_shipping_fee_before_discounts'       => empty($row['AZ'])? '': trim($row['AZ']),
            'seller_shipping_fee_discount'                      => empty($row['BA'])? '': trim($row['BA']),
            'tiktok_Shop_shipping_fee_discount_to_customer'     => empty($row['BB'])? '': trim($row['BB']),
            'related_order_id'                                  => empty($row['AL'])? '' : trim($row['AL']),
            'type'                                              => empty($row['F'])? '' : trim($row['F']),
            'statement_date'                                    => empty($row['A'])? '' : trim($row['A']),
            'statement_id'                                      => empty($row['B'])? '' : trim($row['B']),
            'payment_id'                                        => empty($row['C'])? '' : trim($row['C']),
            'create_time'                                       => empty($row['L'])? '' : trim($row['L']),
        ];
    }




    /**
     * @desc    获取上月数据
     * @date    2024-06-11
     * @author  luckyXiong
     */
    public function get_last_month(){
        $year    = date('Y');
        $month   = date('m')-1;
        if($year=='2025' && $month==1){
            return '202412';
        }else{
            if($month>9){
                return $year.$month;
            }else{
                return $year.'0'.$month;
            }

        }
    }



}